package com.xbongbong.pro.enums.raiders.enums;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.sys.enums.DataPermissionEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 攻略角色权限枚举
 * @author zcp
 * @version v1.0
 * @date 19/9/19 019 20:00
 * @update 19/9/19 019 20:00
 * @since v1.0
 */
@Getter
public enum RaidersPermissionEnum {
    /**
     * 普通销售角色
     */
    SELF(DataPermissionEnum.SELF.getCode(), "enum.raidersPermission.name.self"),

    /**
     * 主管
     */
    DEPARTMENT(DataPermissionEnum.DEPARTMENT.getCode(), "enum.raidersPermission.name.department"),

    /**
     * 超级管理员-全公司
     */
    ALL(DataPermissionEnum.ALL.getCode(), "enum.raidersPermission.name.all"),

    UNKNOWN(404, "enum.raidersPermission.name.unknown"),

    ;

    /**
     * 枚举值 --
     */
    private final int code;
    /**
     * 枚举信息--中文名称
     */
    private final String name;

    RaidersPermissionEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    /**
     * 格式化下拉选项
     * @return com.alibaba.fastjson.JSONArray
     * @author zcp
     * @date 19/9/21 021 14:29
     * @update 19/9/21 021 14:29
     * @since v1.0
     * @version v1.0
     */
    public static JSONArray formatOption() {
        JSONArray jsonArray = new JSONArray();
        for (RaidersPermissionEnum cache : values()) {
            if (Objects.equals(cache, UNKNOWN)) {
                continue;
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key", cache.getCode());
            jsonObject.put("value", cache.getName());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    /**
     * 获取到所有code3
     * @param
     * @return java.util.List<java.lang.Integer>
     * @author zcp
     * @date 19/9/26 026 14:25
     * @update 19/9/26 026 14:25
     * @since v1.0
     * @version v1.0
     */
    public static List<Integer> getAllCode() {
        List<Integer> permission = new ArrayList<>();
        for (RaidersPermissionEnum cache : values()) {
            if (Objects.equals(cache, UNKNOWN)) {
                continue;
            }
            permission.add(cache.code);
        }
        return permission;
    }

    /**
     * 根据登录员工的dataPermission，转换为攻略的permission对应code
     * @param dataPermission 登录员工数据权限
     * @return java.util.List<java.lang.Integer>
     * @author zcp
     * @date 19/9/24 024 19:42
     * @update 19/9/24 024 19:42
     * @since v1.0
     * @version v1.0
     */
    public static List<Integer> getPermission(Integer dataPermission) {
        boolean ifManager = Objects.equals(dataPermission, DataPermissionEnum.DEPARTMENT.getCode()) || Objects.equals(dataPermission, DataPermissionEnum.DEPARTMENT_RECURSION.getCode());
        if (Objects.equals(dataPermission, DataPermissionEnum.ALL.getCode())) {
            //超管看到所有攻略
            return getAllCode();
        } else if (ifManager) {
            //主管看到普通销售和主管的攻略
            return Arrays.asList(DEPARTMENT.code, SELF.code);
        } else {
            //销售只看到让销售看的攻略
            return Arrays.asList(SELF.code);
        }
    }

    public String getName() {
        return I18nMessageUtil.getMessage(name);
    }
}
